home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / C Style Pr199635232001.psc / cPopupMenu.cls < prev   
Encoding:
Visual Basic class definition  |  2001-05-24  |  10.2 KB  |  286 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cPopupMenu"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Option Explicit
  15.  
  16. ' Creates unlimited new popup menus using the API
  17. Implements ISubclass
  18.  
  19. Private m_cNCM As New cNCMetrics
  20.  
  21. ' The messages we will intercept:
  22. Private Const WM_MENUSELECT = &H11F
  23. Private Const WM_MEASUREITEM = &H2C
  24. Private Const WM_DRAWITEM = &H2B
  25. Private Const WM_COMMAND = &H111
  26. Private Const WM_MENUCHAR = &H120
  27. Private Const WM_INITMENUPOPUP = &H117
  28. Private Const WM_WININICHANGE = &H1A
  29. Private Const WM_ENTERMENULOOP = &H211
  30. Private Const WM_EXITMENULOOP = &H212
  31.  
  32. ' Array of menu items
  33. Private m_tMI() As tMenuItem
  34. Private m_iMenuCount As Long
  35.  
  36. ' Display stuff, used to draw the control and also
  37. ' to evaluate menu font item sizes:
  38. Private m_HDC As Long
  39. Private m_hBMPDither As Long
  40. Private m_bUseDither As Boolean
  41. Private m_hFntOld As Long
  42. Private m_bGotFont As Boolean
  43. ' Handle to image list for drawing icons:
  44. Private m_hIml As Long
  45. ' Where to get a tick icon for checked stuff (or -1 to use Win default):
  46. Private m_lTickIconIndex As Long
  47. ' Where to get a option button icon for checked stuff (or -1 to use Win default)
  48. Private m_lOptionIconIndex As Long
  49.  
  50. ' hWNd of owner:
  51. Private m_hWndOwner As Long
  52. Private m_hWndAttached As Long
  53. ' Height of a menu item:
  54. Private m_lMenuItemHeight As Long
  55.  
  56. ' Bitmap to tile into background of menu:
  57. Private m_hDCBack As Long
  58. Private m_hBmpOld As Long
  59. Private m_hBmp As Long
  60. Private m_lBitmapW As Long
  61. Private m_lBitmapH As Long
  62.  
  63. ' Sub menus:
  64. Private m_lSubMenuCount As Long
  65. Private m_hSubMenus() As Long
  66. ' Next id to choose for a menu item:
  67. Private m_lLastMaxId As Long
  68.  
  69. Private m_bGradientHighlight As Boolean
  70.  
  71. Private m_sTag As String
  72. Private m_bDrawHeadersAsSeparators As Boolean
  73.  
  74. Public Enum ECNMHeaderStyle
  75.    ecnmHeaderCaptionBar = 0
  76.    ecnmHeaderSeparator = 1
  77. End Enum
  78.  
  79. ' Events:
  80. Public Event Click(ItemNumber As Long)
  81. Attribute Click.VB_Description = "Fired when a menu item is clicked AND the CreateSubClass method has been called since the menu was last shown.  Normally the return value of the ShowPopupMenu event tells you which item is clicked."
  82. Public Event ItemHighlight(ItemNumber As Long, bEnabled As Boolean, bSeparator As Boolean)
  83. Attribute ItemHighlight.VB_Description = "Raised when an item is highlighted in a pop-up menu."
  84. Public Event MenuExit()
  85. Attribute MenuExit.VB_Description = "Raised when a popup menu is exited."
  86. Public Event InitPopupMenu(ParentItemNumber As Long)
  87. Attribute InitPopupMenu.VB_Description = "Raised when a submenu is about to be shown.  You can modify the pop-up menu's contents in this event without any problem."
  88. Public Event DrawItem(ByVal hdc As Long, ByVal lMenuIndex As Long, ByRef lLeft As Long, ByRef lTop As Long, ByRef lRight As Long, ByRef lBottom As Long, ByVal bSelected As Boolean, ByVal bChecked As Boolean, ByVal bDisabled As Boolean, bDoDefault As Boolean)
  89. Public Event MeasureItem(ByVal lMenuIndex As Long, ByRef lWidth As Long, ByRef lHeight As Long)
  90.  
  91. Public Property Set BackgroundPicture( _
  92.       ByRef sPic As StdPicture _
  93.    )
  94. Attribute BackgroundPicture.VB_Description = "Sets a StdPicture object to tile behind the menu items.  Use ClearBackgroundPicture to remove the picture again."
  95. Dim tBm As BITMAP
  96. Dim lHDC As Long
  97. Dim lHDCTemp As Long
  98. Dim lHBmpTemp As Long
  99. Dim bBackOk As Boolean
  100.  
  101.    ClearBackgroundPicture
  102.    GetObjectAPI sPic.Handle, Len(tBm), tBm
  103.    m_lBitmapW = tBm.bmWidth
  104.    m_lBitmapH = tBm.bmHeight
  105.    If m_lBitmapW > 0 And m_lBitmapH > 0 Then
  106.       lHDC = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
  107.       If lHDC <> 0 Then
  108.          lHDCTemp = CreateCompatibleDC(lHDC)
  109.          If lHDCTemp <> 0 Then
  110.             lHBmpTemp = SelectObject(lHDCTemp, sPic.Handle)
  111.             If lHBmpTemp <> 0 Then
  112.                m_hDCBack = CreateCompatibleDC(lHDC)
  113.                If m_hDCBack <> 0 Then
  114.                   m_hBmp = CreateCompatibleBitmap(lHDC, m_lBitmapW, m_lBitmapH)
  115.                   If m_hBmp <> 0 Then
  116.                      m_hBmpOld = SelectObject(m_hDCBack, m_hBmp)
  117.                      If m_hBmpOld <> 0 Then
  118.                         BitBlt m_hDCBack, 0, 0, m_lBitmapW, m_lBitmapH, lHDCTemp, 0, 0, vbSrcCopy
  119.                         bBackOk = True
  120.                      End If
  121.                   End If
  122.                End If
  123.                SelectObject lHDCTemp, lHBmpTemp
  124.             End If
  125.             DeleteDC lHDCTemp
  126.          End If
  127.          DeleteDC lHDC
  128.       End If
  129.    End If
  130.    If Not bBackOk Then
  131.       ClearBackgroundPicture
  132.    End If
  133.       
  134. End Property
  135. Public Sub ClearBackgroundPicture()
  136. Attribute ClearBackgroundPicture.VB_Description = "Removes the background picture (if any) being used to tile behind menu items."
  137.    If m_hBmpOld <> 0 Then
  138.       SelectObject m_HDC, m_hBmpOld
  139.       m_hBmpOld = 0
  140.    End If
  141.    If m_hBmp <> 0 Then
  142.       DeleteObject m_hBmp
  143.    End If
  144.    If m_hDCBack <> 0 Then
  145.       DeleteDC m_hDCBack
  146.    End If
  147.    m_hDCBack = 0
  148.    m_lBitmapW = 0
  149.    m_lBitmapH = 0
  150. End Sub
  151.  
  152. Friend Function AcceleratorPress(ByVal nKeyCode As KeyCodeConstants, ByVal wMask As ShiftConstants) As Boolean
  153. Dim i As Long
  154.    For i = 1 To m_iMenuCount
  155.       If Not m_tMI(i).iShortCutShiftKey = 0 Then
  156.          'Debug.Print "Accel Press..."; nKeyCode, wMask, m_tMI(i).iShortCutShiftKey, m_tMI(i).iShortCutShiftMask
  157.          If m_tMI(i).iShortCutShiftMask = wMask Then
  158.             If m_tMI(i).iShortCutShiftKey = nKeyCode Then
  159.                ' Yo!
  160.                RaiseEvent Click(i)
  161.                AcceleratorPress = True
  162.                Exit For
  163.             End If
  164.          End If
  165.       End If
  166.    Next i
  167. End Function
  168. Public Property Get IDForItem(ByVal lIndex As Long) As Long
  169. Attribute IDForItem.VB_Description = "Returns the Menu ID used to identify a menu item.  If the menu has a child menu, this will be the menu handle of the child menu."
  170.    If lIndex > 0 And lIndex <= m_iMenuCount Then
  171.       IDForItem = m_tMI(lIndex).lActualID
  172.    End If
  173. End Property
  174. Public Property Get ItemForID(ByVal wID As Long) As Long
  175. Attribute ItemForID.VB_Description = "Returns the Index of the menu item with the specified ID."
  176. Dim lIndex As Long
  177.    For lIndex = 1 To m_iMenuCount
  178.       If m_tMI(lIndex).lActualID = wID Then
  179.          ItemForID = lIndex
  180.          Exit For
  181.       End If
  182.    Next lIndex
  183. End Property
  184.  
  185. Public Sub EmulateMenuClick(ByVal wID As Long)
  186. Attribute EmulateMenuClick.VB_Description = "Given the ID of a menu item, calls the code cPopupMenu would normally run when the item is clicked."
  187. Dim lIndex As Long
  188.    For lIndex = 1 To m_iMenuCount
  189.       If m_tMI(lIndex).lActualID = wID Then
  190.          RaiseClickEvent wID
  191.          Exit For
  192.       End If
  193.    Next lIndex
  194. End Sub
  195.  
  196. Public Property Get GradientHighlight() As Boolean
  197. Attribute GradientHighlight.VB_Description = "Gets/sets whether highlights on the menu are drawn with a gradient or not."
  198.    GradientHighlight = m_bGradientHighlight
  199. End Property
  200. Public Property Let GradientHighlight(ByVal bState As Boolean)
  201.    m_bGradientHighlight = bState
  202. End Property
  203.  
  204. Public Property Get HeaderStyle() As ECNMHeaderStyle
  205. Attribute HeaderStyle.VB_Description = "Gets/sets how header style menu items will be drawn.  Header style items can either be drawn in an ICQ-style (when a standard menu separator is drawn but the text is rendered in a small font) or in a small window caption style."
  206.    If (m_bDrawHeadersAsSeparators) Then
  207.       HeaderStyle = ecnmHeaderSeparator
  208.    Else
  209.       HeaderStyle = ecnmHeaderCaptionBar
  210.    End If
  211. End Property
  212. Public Property Let HeaderStyle(ByVal eStyle As ECNMHeaderStyle)
  213.    If (eStyle = ecnmHeaderCaptionBar) Then
  214.       m_bDrawHeadersAsSeparators = False
  215.    Else
  216.       m_bDrawHeadersAsSeparators = True
  217.    End If
  218. End Property
  219.  
  220. Public Property Get Count() As Long
  221. Attribute Count.VB_Description = "Gets the number of items in the menu."
  222.    Count = m_iMenuCount
  223. End Property
  224.  
  225. Public Property Get HighlightCheckedItems() As Boolean
  226. Attribute HighlightCheckedItems.VB_Description = "Gets/sets whether checked items should be highlighted when the menu item is selected."
  227.     HighlightCheckedItems = m_bUseDither
  228. End Property
  229. Public Property Let HighlightCheckedItems(ByVal bState As Boolean)
  230.     m_bUseDither = bState
  231.     If (bState) Then
  232.         ' Get the dither bitmap from the resource file:
  233.         m_hBMPDither = LoadImageByNum(App.hInstance, 49, IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS)
  234.     Else
  235.         If (m_hBMPDither <> 0) Then
  236.             DeleteObject m_hBMPDither
  237.             m_hBMPDither = 0
  238.         End If
  239.     End If
  240. End Property
  241. Public Property Get Tag() As String
  242. Attribute Tag.VB_Description = "Gets/sets a string associated with the popup-menu object."
  243.    Tag = m_sTag
  244. End Property
  245. Public Property Let Tag(ByVal sTag As String)
  246.    m_sTag = sTag
  247. End Property
  248. Public Property Get hWndOwner() As Long
  249. Attribute hWndOwner.VB_Description = "Sets the owning window of the popup menu.  This must be set before any popup menus are shown."
  250.    hWndOwner = m_hWndOwner
  251. End Property
  252. Public Property Let hWndOwner(ByVal hWndA As Long)
  253. Dim lHDC As Long
  254.    
  255.    ' Clear up:
  256.    Clear
  257.    ' Clear DC:
  258.    If (m_HDC <> 0) Then
  259.        If (m_hFntOld <> 0) Then
  260.            SelectObject m_HDC, m_hFntOld
  261.        End If
  262.        DeleteObject m_HDC
  263.    End If
  264.  
  265.    ' Set for new owner:
  266.    m_hWndOwner = hWndA
  267.    lHDC = GetDC(hWndA)
  268.    m_HDC = CreateCompatibleDC(lHDC)
  269.    ReleaseDC m_hWndOwner, lHDC
  270.    ' Select the menu font into it:
  271.    pSelectMenuFont
  272.    
  273. End Property
  274.  
  275. Public Property Let ImageList( _
  276.         ByRef vImageList As Variant _
  277.     )
  278. Attribute ImageList.VB_Description = "Associates an ImageList with the Popup menu for setting icons.  This may be set to either a VB ImageList control or a hImageList API handle."
  279.     If (VarType(vImageList) = vbLong) Then
  280.         ' Assume a handle to an image list:
  281.         m_hIml = vImageList
  282.     ElseIf (VarType(vImageList) = vbObject) Then
  283.         ' Assume a VB image list:
  284.         On Err 
  285.         OIerty
  286. Public PropeE